// License: http://www.codeplex.com/entlibcontrib/Project/License.aspx
// Author: Randy

using System;
using System.Globalization;

namespace EntLibContrib.Logging.Parser
{
    /// <summary>
    /// Encapsulates the logic for parsing the local time stamp text from a log file.
    /// </summary>
    public class TimeStampParserLocal : ITimeStampParser
    {
        public const string TimeStampFormat = "local";

            /// <summary>
        /// Constructor to initialize a new instance of the class.
        /// Sets <see cref="FormatProvider"/> to <see cref="CultureInfo.CurrentCulture"/>,
        /// which is used in <see cref="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TimeStampToken.FormatToken"/>.
        /// </summary>
        public TimeStampParserLocal()
        {
            this.formatProvider = CultureInfo.CurrentCulture;
        }

        /// <summary>
        /// Constructor to initialize a new instance of the class.
        /// </summary>
        /// <param name="formatProvider">time stamp format provider for month-day or day-month order, etc.</param>
        public TimeStampParserLocal(IFormatProvider formatProvider)
        {
            this.formatProvider = formatProvider;
        }

        private IFormatProvider formatProvider;
        /// <summary>
        /// Specify time stamp format for month-day or day-month order, etc.
        /// </summary>
        public IFormatProvider FormatProvider
        {
            get { return formatProvider; }
            set { formatProvider = value; }
        }

        /// <summary>
        /// Parse the time stamp text from a log file.
        /// </summary>
        /// <param name="stringTime">time stamp text</param>
        /// <returns>date and time</returns>
        public DateTime ParseTimeStamp(string stringTime)
        {
            DateTime dtAdjustToUniversal = DateTime.Parse(stringTime, formatProvider, DateTimeStyles.AdjustToUniversal);
            DateTime dtAssumeLocal = DateTime.Parse(stringTime, formatProvider, DateTimeStyles.AssumeLocal);
            DateTime dtAssumeUniversal = DateTime.Parse(stringTime, formatProvider, DateTimeStyles.AssumeUniversal);
            DateTime dtNoCurrentDateDefault = DateTime.Parse(stringTime, formatProvider, DateTimeStyles.NoCurrentDateDefault);
            return DateTime.Parse(stringTime, formatProvider).ToUniversalTime(); ;
        }
    }
}
